package com.codefish;

/**
 * A股票分析者
 *
 * @author codefish
 * @version 1.0
 * @date 2022/05/07 下午 04:30
 */
public class ReportObserver extends StockObserver {
    /**
     * 订阅的发布者
     */
    StockPublisher publisher;

    /**
     * 构造函数
     * @param name 订阅者标识
     * @param publisher 订阅的发布者对象
     */
    public ReportObserver(String name,StockPublisher publisher) {
        super.name = name;
        this.publisher = publisher;
    }

    double minPrice = Double.MAX_VALUE;
    double avgPrice = 0;
    int days = 0;
    double maxPrice = -1;

    @Override
    public void execute() {
        //如果订阅的主题是A股票，则更新自身信息
        if(publisher instanceof AStockPublisher){
            double price = ((AStockPublisher) publisher).getPrice();
            minPrice = Math.min(price, minPrice);
            maxPrice = Math.max(price, maxPrice);
            avgPrice = (avgPrice * days + price) / (days + 1);
            days++;
            System.out.printf("%s:股票最小值:%f；股票最大值:%f；股票平均值:%f；\n", name, minPrice, maxPrice, avgPrice);
        }
    }
}
